angular.module('starter.controllers')
.controller('PlacerepairCtrl', function($scope, museumFac, $stateParams, $timeout, $location, cfg, $cookies, uploadImg) {
	$scope.change_language = function(id){
	    $scope.language = id;
	    var expireDate = new Date();  
		expireDate.setDate(expireDate.getDate() + 30);                
		$cookies.put('language', id, {'expires': expireDate.toUTCString()});
		location.reload();
		if(id == 0){
			document.getElementById('title').innerHTML = '提示';
			document.getElementById('windowclosebutton').value = '确定';
		}else{
			document.getElementById('title').innerHTML = 'Hint';
			document.getElementById('windowclosebutton').value = 'Confirm';
		}

	}

	$scope.getLanguage = function(){
    	$scope.language = $cookies.get('language');
    	if($scope.language){
      		$scope.language = $scope.language;
    	}else{
      		$scope.language = 0;
    	}
    	if($scope.language == 0){
			document.getElementById('title').innerHTML = '提示';
			document.getElementById('windowclosebutton').value = '确定';
		}else{
			document.getElementById('title').innerHTML = 'Hint';
			document.getElementById('windowclosebutton').value = 'Confirm';
		}
  	}
  	$scope.getLanguage();

	$scope.checkOpenid = function(){
        $scope.url = window.location.href;
        wx.ready(function(){
          wx.onMenuShareTimeline({
            title: '四方当代美术馆 | 线上商店', // 分享标题
            desc: ' ', // 分享描述
            link: $scope.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: 'http://store.sifangartmuseum.org.cn/Public/museum/www/img/logo.png', // 分享图标
            success: function () { 
                // 用户确认分享后执行的回调函数
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
            }
        });
        // 获取“分享给朋友”按钮点击状态及自定义分享内容接口
        wx.onMenuShareAppMessage({
            title: '四方当代美术馆 | 线上商店',  // 分享标题
            desc: ' ', // 分享描述
            link: $scope.url, // 分享链接，该链接域名或路径必须与当前页面对应的公众号JS安全域名一致
            imgUrl: 'http://store.sifangartmuseum.org.cn/Public/museum/www/img/logo.png', // 分享图标
            type: '', // 分享类型,music、video或link，不填默认为link
            dataUrl: '', // 如果type是music或video，则要提供数据链接，默认为空
            success: function () { 
                // 用户确认分享后执行的回调函数
            },
            cancel: function () { 
                // 用户取消分享后执行的回调函数
            }
          });
        });
	}
	$scope.checkOpenid();

    $scope.getinfo = function(){
        museumFac.product.addressdetail({
            addressId:$stateParams.id
        },function(data){
            $scope.user = data.info;
            if($scope.user.province_id == 1||$scope.user.province_id == 20||$scope.user.province_id == 860||$scope.user.province_id == 2462){
                $scope.city_show_name = $scope.user.province_name + ' ' + $scope.user.district_name;
            }else{
                $scope.city_show_name = $scope.user.province_name + ' ' + $scope.user.city_name + ' ' + $scope.user.district_name;
            }
          },function(data){
            alert(data.data.info);
        });
    }
    $scope.getinfo();

	$scope.choen = function(flag){
        $scope.inputc = flag;
        if(flag == 3){
            $scope.getprovince();
            $scope.mubu1 = 1;
        }
    }

    $scope.getprovince = function(){
        museumFac.product.cities(function(data){
            $scope.citys = data.info;
          },function(data){
            alert(data.data.info);
        });
    }

    $scope.cancle_city = function(){
        $scope.mubu1 = 0;
    }

    $scope.cancle_city1 = function(){
        $scope.mubu1 = 1;
    }

    $scope.cancle_city2 = function(){
        $scope.mubu1 = 2;
    }

    $scope.showcity = function(list){
        $scope.province_name = list.name;
        $scope.province_id = list.id;
        museumFac.product.cities({
            pid:list.id
        },function(data){
            $scope.citys1 = data.info;
            $scope.mubu1 = 2;
          },function(data){
            alert(data.data.info);
        });
    }

    $scope.showcity1 = function(list){
        $scope.city_name = list.name;
        $scope.city_id = list.id;
        museumFac.product.cities({
            pid:list.id
        },function(data){
            $scope.citys2 = data.info;
            $scope.mubu1 = 3;
          },function(data){
            alert(data.data.info);
        });
    }

    $scope.showcity2 = function(list){
        $scope.district_name = list.name;
        $scope.district_id = list.id;
        $scope.mubu1 = 0;
        if($scope.province_id == 1||$scope.province_id == 20||$scope.province_id == 860||$scope.province_id == 2462){
            $scope.city_show_name = $scope.province_name + ' ' + $scope.district_name;
        }else{
            $scope.city_show_name = $scope.province_name + ' ' + $scope.city_name + ' ' + $scope.district_name;
        }
        $scope.user1.province = $scope.province_id;
        $scope.user1.city = $scope.city_id;
        $scope.user1.district = $scope.district_id;
    }

    $scope.cancleaddress = function(){
        $location.url('placelist');
    }

    $scope.user1 = {};
    $scope.addaddress = function(){
        $scope.user1.phone = $scope.user.phone;
        $scope.user1.name = $scope.user.name;
        $scope.user1.addressId = $scope.user.id;
        $scope.user1.area = $scope.user.area;
        if(!$scope.user1.province){
            $scope.user1.province = $scope.user.province;
        }
        if(!$scope.user1.city){
            $scope.user1.city = $scope.user.city;
        }
        if(!$scope.user1.district){
            $scope.user1.district = $scope.user.district;
        }
        museumFac.product.editaddress($scope.user1,function(data){
            alert('保存成功');
            $location.url('placelist');
          },function(data){
            alert(data.data.info);
        });
    }

    $scope.changecheck = function(){
        if($scope.user.is_default == 0){
            $scope.user.is_default = 1;
            $scope.user1.is_default = $scope.user.is_default;
        }else{
            $scope.user.is_default = 0;
            $scope.user1.is_default = $scope.user.is_default;
        }
    }
  
})